﻿// 
// Copyright (c) 2013 Jason Bell
// 
// Permission is hereby granted, free of charge, to any person obtaining a 
// copy of this software and associated documentation files (the "Software"), 
// to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, 
// and/or sell copies of the Software, and to permit persons to whom the 
// Software is furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included 
// in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
// 

using System;

using Gharen.GLCore;

namespace ExampleDraw
{
    class ShadowMap
        : IDisposable
    {
        int framebuffer;
        public int depthTexture;

        GLES30 gl;

        public ShadowMap(GLES30 gl)
        {
            this.gl = gl;

            this.framebuffer = gl.GenFramebuffer();
            gl.BindFramebuffer(gl.GL_FRAMEBUFFER, this.framebuffer);

            this.depthTexture = gl.GenTexture();
            gl.BindTexture(gl.GL_TEXTURE_2D, this.depthTexture);
            gl.TexImage2D(gl.GL_TEXTURE_2D,
                0,
                gl.GL_DEPTH_COMPONENT16,
                1024, 1024,
                0,
                gl.GL_DEPTH_COMPONENT,
                gl.GL_FLOAT,
                IntPtr.Zero);
            gl.TexParameter(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MIN_FILTER, gl.GL_NEAREST);
            gl.TexParameter(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER, gl.GL_NEAREST);
            gl.TexParameter(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_WRAP_S, gl.GL_CLAMP_TO_EDGE); 
            gl.TexParameter(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_WRAP_T, gl.GL_CLAMP_TO_EDGE); 

            gl.FramebufferTexture2D(gl.GL_FRAMEBUFFER, gl.GL_DEPTH_ATTACHMENT, gl.GL_TEXTURE_2D, depthTexture, 0);

            gl.DrawBuffer(gl.GL_NONE);
            gl.ReadBuffer(gl.GL_NONE);

            int err = gl.CheckFramebufferStatus(gl.GL_FRAMEBUFFER);
            Console.WriteLine(err);

            gl.BindFramebuffer(gl.GL_FRAMEBUFFER, 0);
        }

        public void Dispose()
        {
            if(this.framebuffer != 0)
            {
                gl.DeleteTexture(this.depthTexture);
                gl.DeleteFramebuffer(this.framebuffer);
                this.framebuffer = 0;
            }
        }

        public void Bind()
        {
            gl.BindFramebuffer(gl.GL_FRAMEBUFFER, this.framebuffer);
        }

        public void Unbind()
        {
            gl.BindFramebuffer(gl.GL_FRAMEBUFFER, 0);
        }
    }
}
